/**
 * 
 */
package org.pump.client.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.pump.client.codec.Packet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mysql.jdbc.StringUtils;

/**
 * @author liudong03
 *
 */
public class TypeUtil {
	private static final Logger mylog = LoggerFactory.getLogger(TypeUtil.class);
	public static final int DEC_SIZE = 9;
	public static int dig2bytes[] = {0,1,1,2,2,3,3,4,4,4};
	
	/**
	 * 
	 */
	public TypeUtil() {
		// TODO Auto-generated constructor stub
	}
	
	public static byte[] scramble411(String password, String seed,String passwordEncoding)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
		
		byte[] passwordHashStage1 = md
				.digest((passwordEncoding == null || passwordEncoding.length() == 0) ? 
						StringUtils.getBytes(password)
						: StringUtils.getBytes(password, passwordEncoding));
		md.reset();

		byte[] passwordHashStage2 = md.digest(passwordHashStage1);
		md.reset();

		byte[] seedAsBytes = StringUtils.getBytes(seed, "ASCII"); // for debugging
		md.update(seedAsBytes);
		md.update(passwordHashStage2);

		byte[] toBeXord = md.digest();

		int numToXor = toBeXord.length;

		for (int i = 0; i < numToXor; i++) {
			toBeXord[i] = (byte) (toBeXord[i] ^ passwordHashStage1[i]);
		}

		return toBeXord;
	}
	public static int getCharsetID(String charset)
	{
		int charsetid = -1;
		if(charset.equals("gbk"))
		{
			charsetid = 28;
		}else
		{
			mylog.error("unsupported charset");
		}
		return charsetid;
	}

	public static long calc_meta(byte[] meta)
	{
		if(meta == null)
		{
			return -1;
		}
		
		long result = 0;
		int sz = meta.length;
		for(int i = 0;i < sz; i++)
		{
			long curbytenum = meta[i] & 0xff;
			result += (curbytenum << (i*8));
		}
		return result;
	}
	
	public static int calc_decimal_value(byte[] data, int start, int len)
	{
		int result = 0;
		
		if(len == 1)
		{
			result = data[start];
		}else if(len == 2)
		{
			for(int i = 0; i < len; i++)
			{
				result = result | ((int)(data[start + i] & 0xff) << (len-i-1)*8);
			}
		}else if(len == 3)
		{
			for(int i = 0; i < len; i++)
			{
				result = result | ((int)(data[start + i] & 0xff) << (len - i -1) * 8);
			}
			if((data[0] & 0x80) != 0)
			{
				result = result | ((int)255 << 24);
			}
		}else if(len == 4)
		{
			for(int i = 0; i < len; i++)
			{
				result = result | ((int)(data[start + i] & 0xff) << (len - i -1) * 8);
			}
		}else
		{
			mylog.error("unsupported length");
		}
		
		return result;
	}
	
	public static int calc_decimal_bytes(int precision,int scale)
	{
		int intg = precision - scale;
		int intg0 = intg/DEC_SIZE;
		int frac0 = scale/DEC_SIZE;
		int intg0x = intg - intg0 * DEC_SIZE;
		int frac0x = scale - frac0 * DEC_SIZE;
		
		int result = intg0 * 4 + dig2bytes[intg0x] + frac0 * 4 + dig2bytes[frac0x];
		return result;
	}
}
